package cn.retalsystem.controller;

import cn.retalsystem.bean.Admin;
import cn.retalsystem.bean.Buding;
import cn.retalsystem.bean.Room;
import cn.retalsystem.service.BudingService;
import cn.retalsystem.service.RoomService;
import cn.retalsystem.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @Author: 官宝星
 * @Date: 2019/11/5 17:23
 * @Version 1.0
 * @描述：
 */
@Controller
@RequestMapping("/room")
public class RoomController {

    @Autowired
    RoomService roomService;
    @Autowired
    BudingService budingService;

    /**
     * 跳转到房间信息管理界面
     * @return String
     */
    @RequestMapping("/toRoomView")
    public String toRoomView(Model model,String rname,Integer budingId){
        //获取楼栋信息，填充下拉选择框
        List<Buding> budings = budingService.selectAllBuding();
        model.addAttribute("bs",budings);
        //获取房间信息总数,用于渲染分页
        Integer count = roomService.selectCountByExample(rname, budingId);
        model.addAttribute("count",count);
        //将条件查询的信息存入mode，用于分页异步请求
        model.addAttribute("rname",rname);
        model.addAttribute("budingId",budingId);
        return "room";
    }



    /**
     * 查询所有房间信息
     * @return
     */
    @RequestMapping("/getAllRoomVo")
    @ResponseBody
    public List<RoomVo> getAllRoomVo(){
        return roomService.selectAllRoomVO();
    }

    /**
     * 查询房间信息总数
     * @return
     */
    @RequestMapping("/getCount")
    @ResponseBody
    public Integer getCount(){
        return roomService.selectCount();
    }

    /**
     * 分页查询房间信息
     * @param index
     * @param size
     * @return
     */
    @RequestMapping("/getRoomByPage")
    @ResponseBody
    public List<RoomVo> getRoomVoByPage(Integer index,Integer size){
        return roomService.selectRoomVoByPage(index,size);
    }

    /**
     * 根据查询条件分页查询房间信息
     * @param rname 房间名称
     * @param budingId 楼栋id
     * @param index 当前页
     * @param size 页面数据总量
     * @return 房间信息集合
     */
    @RequestMapping("/getRoomByPageAndExample")
    @ResponseBody
    public List<RoomVo> getRoomVoByPageAndExample(String rname,Integer budingId,Integer index,Integer size){
        return roomService.selectRoomByExampleAndPage(rname,budingId,index,size);
    }

    /**
     * 通过id集合删除房间信息
     * @param map id集合的键值对
     * @return
     */
    @RequestMapping("/delRoomById")
    @ResponseBody
    public boolean delRoom(@RequestBody Map<String,List<Integer>> map){
        List<Integer> ids = map.get("ids");
        roomService.delRoomById(ids);
        return true;
    }

    /**
     * 添加房间信息
     * @param room 房间对象
     * @param session session作用域，用于取admin的id
     * @return 执行成功返回true
     */
    @RequestMapping("/addRoom")
    @ResponseBody
    public boolean addRoom(@RequestBody Room room, HttpSession session){
        Admin admin = (Admin) session.getAttribute("admin");
        roomService.addRoom(room);
        return true;
    }

    /**
     * 根据id查询房间信息
     * @param id 房间id
     * @return 房间对象
     */
    @RequestMapping("/getRoomById")
    @ResponseBody
    public Room getRoomById(Integer id){
        return roomService.selectRoomById(id);
    }

    @RequestMapping("/updateRoom")
    @ResponseBody
    public boolean updateRoom(@RequestBody Room room){
        roomService.updateRoom(room);
        return true;
    }
}
